<?php
/**
 * Created by PhpStorm.
 * User: zhanglu
 * Date: 2018/12/12
 * Time: 9:16 PM
 */
namespace console\controllers;

use common\components\Tools;
use yii\console\Controller;
use common\components\Config;
use backend\modules\home\models\MhEmail;
use Yii;

class DefaultController extends Controller{

    public function actionIndex(){
        // echo "hello world";
        // echo "\n";
        $this->sendMail();
    }

    public function sendMail(){

        $to = Config::getValue('email_to');
        if(!$to){
            return true;
        }

        $list = MhEmail::find()
            ->where(['status' => 0])
            ->limit(2)
            ->all();
        if(count($list) > 0){
            foreach($list as $v){
                $mail= Yii::$app->mailer->compose();
                $mail->setTo($to);
                $mail->setSubject("申请资料索取提醒");
                $html = $this->getHtml($v);
                $mail->setHtmlBody($html);    //发布可以带html标签的文本
                if($mail->send()){
                    $v->status = 1;
                    $v->save();
                    continue;
                }else{
                    echo 'fail';exit;
                }
            }
            return true;
        }else{
            return true;
        }
    }

    public function getHtml($v){
        $proince = '';
        $city = '';
        if($p = Tools::getProvince($v->province) ){
            $proince = $p->province;
        }
        if($c = Tools::getCity($v->city) ){
            $city = $c->city;
        }
        $time = date('Y-m-d H:i:s',$v->create_time);
        $str = <<<EOT
<table width="100%">
	<caption>客户资料索取</caption>
	<tbody>
		<tr>
			<td width="120" align="right">联系人名称：</td>
			<td>{$v->name}</td>
		</tr>
		<tr>
			<td width="120" align="right">联系人邮箱：</td>
			<td>{$v->email}</td>
		</tr>
		<tr>
			<td width="120" align="right">联系人电话：</td>
			<td>{$v->telephone}</td>
		</tr>
		<tr>
			<td width="120" align="right">公司名称：</td>
			<td>{$v->company}</td>
		</tr>
		<tr>
			<td width="120" align="right">所在城市：</td>
			<td>{$proince} - {$city}</td>
		</tr>
		<tr>
			<td width="120" align="right">需求：</td>
			<td>{$v->interest}</td>
		</tr>
		<tr>
			<td width="120" align="right">备注：</td>
			<td>{$v->note}</td>
		</tr>
		<tr>
			<td width="120" align="right">创建时间：</td>
			<td>{$time}</td>
		</tr>
	</tbody>
</table>
EOT;
        return $str;
    }

}